package cn.dakaqi.services.impl;

import cn.dakaqi.dao.ActivityRecruitVolunteerDao;
import cn.dakaqi.entities.*;
import cn.dakaqi.entities.user.Volunteer;
import cn.dakaqi.enumerate.CommonStatusCode;
import cn.dakaqi.services.*;
import cn.dakaqi.services.user.VolunteerService;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import cn.dakaqi.utils.rongYun.PushActivityMesssage;
import cn.dakaqi.utils.rongYun.PushGroupMesssage;
import cn.dakaqi.utils.rongYun.PushSystemMesssage;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * @author chunyang on 2016/4/19.
 */
@Component(value = "activityRecruitVolunteerService")
@Transactional
public class ActivityRecruitVolunteerServiceImpl implements ActivityRecruitVolunteerService {
    @Autowired
    ActivityService activityService;
    @Autowired
    GroupVolunteerService groupVolunteerService;
    @Autowired
    ActivityRecruitVolunteerDao activityRecruitVolunteerDao;
    @Autowired
    ActivityRecruitService activityRecruitService;
    @Autowired
    VolunteerService volunteerService;
    @Autowired
    ActivityApplyService activityApplyService;
    @Autowired
    private LogService logService;

    private void saveLog(String message) {
        logService.save(400, message);
    }

    @Override
    public void create(String param) throws ServiceRuntimeException {
        JSONObject jsonObject = JSON.parseObject(param);
        String memberCode = jsonObject.getString("memberCode");
        Long activityRecruitId = jsonObject.getLong("activityRecruitId");
        String answer = jsonObject.getString("answer");
        String question = jsonObject.getString("question");

        ActivityRecruitVolunteer activityRecruitVolunteer = new ActivityRecruitVolunteer();
        activityRecruitVolunteer.setStatus(DKQConstant.APPLY_STATUS);
        activityRecruitVolunteer.setCreateTime(Clock.DEFAULT.getCurrentDate());
        activityRecruitVolunteer.setActivityRecruit(this.activityRecruitService.findOne(activityRecruitId));
        //检查当前用户是否完善个人资料
        Volunteer volunteer = this.volunteerService.findByMemberCode(memberCode);
        if (!Volunteer.checkInfo(volunteer)) {
            throw new ServiceRuntimeException(CommonStatusCode.USER_INFO_NOT_FULL.getStatusCode(),
                    CommonStatusCode.USER_INFO_NOT_FULL.getStatusMessage());
        }
        //检查当前用户是否本次招募社团的成员
        Activity activity = activityRecruitVolunteer.getActivityRecruit().getActivity();
        GroupVolunteer groupVolunteer = this.groupVolunteerService.findByMemberAndGroup(memberCode, DKQConstant.APPLY_STATUS_OK, activity.getGroup().getGroupCode());
        if (null == groupVolunteer) {
            activityRecruitVolunteer.setStatus(DKQConstant.APPLY_STATUS_OK);
        }
        activityRecruitVolunteer.setVolunteer(volunteer);
        activityRecruitVolunteer.setAnswer(answer);
        activityRecruitVolunteer.setQuestion(question);
        activityRecruitVolunteer.setStatus(DKQConstant.APPLY_STATUS);
        this.save(activityRecruitVolunteer);
    }

    @Override
    public ActivityApply apply(String memberCode, String activityCode, String answer) {
        //检查当前用户是否完善个人资料
        Volunteer volunteer = this.volunteerService.findByMemberCode(memberCode);
        if (!Volunteer.checkInfo(volunteer)) {
            throw new ServiceRuntimeException(CommonStatusCode.USER_INFO_NOT_FULL.getStatusCode(),
                    CommonStatusCode.USER_INFO_NOT_FULL.getStatusMessage());
        }
        Activity activity = activityService.findByActivityCode(activityCode);

        if (null == answer || StringUtils.isBlank(answer) || "null".equals(answer)) {
            GroupVolunteer groupVolunteer = this.groupVolunteerService.findByMemberAndGroup(memberCode, DKQConstant.APPLY_STATUS_OK, activity.getGroup().getGroupCode());
            if (null == groupVolunteer) {
                throw new ServiceRuntimeException(CommonStatusCode.ACTIVITY_NOT_OUTSIDE_RECRUIT.getStatusCode(),
                        CommonStatusCode.ACTIVITY_NOT_OUTSIDE_RECRUIT.getStatusMessage());
            } else {
                if (groupVolunteer.getStatus() == DKQConstant.APPLY_STATUS_FREEZE) {
                    throw new ServiceRuntimeException(CommonStatusCode.USER_IS_FROST.getStatusCode(),
                            CommonStatusCode.USER_IS_FROST.getStatusMessage() + ",不可以报名活动");
                }
            }
            ActivityApply activityApply = new ActivityApply();
            activityApply.setCreateTime(Clock.DEFAULT.getCurrentDate());
            activityApply.setVolunteer(volunteer);
            activityApply.setActivity(activity);
            activityApply.setRole(DKQConstant.ROLES_VOLUNTEER);
            activityApply.setStatus(DKQConstant.APPLY_STATUS_OK);
            return this.activityApplyService.save(activityApply);
        } else {
            ActivityRecruit activityRecruit = this.activityRecruitService.findByActivityCode(activityCode);
            ActivityRecruitVolunteer activityRecruitVolunteer = new ActivityRecruitVolunteer();
            activityRecruitVolunteer.setStatus(DKQConstant.APPLY_STATUS);
            activityRecruitVolunteer.setCreateTime(Clock.DEFAULT.getCurrentDate());
            activityRecruitVolunteer.setActivityRecruit(activityRecruit);
            activityRecruitVolunteer.setVolunteer(volunteer);
            activityRecruitVolunteer.setAnswer(answer);
            activityRecruitVolunteer.setQuestion(activityRecruit.getQuestion());
            activityRecruitVolunteer.setStatus(DKQConstant.APPLY_STATUS);
            return this.saveApply(activityRecruitVolunteer);
        }
    }

    @Override
    public ActivityApply apply(String param) throws ServiceRuntimeException {
        JSONObject jsonObject = JSON.parseObject(param);
        String memberCode = jsonObject.getString("memberCode");
        String activityCode = jsonObject.getString("activityCode");
        String answer = jsonObject.getString("answer");

        //检查当前用户是否完善个人资料
        Volunteer volunteer = this.volunteerService.findByMemberCode(memberCode);
        if (!Volunteer.checkInfo(volunteer)) {
            throw new ServiceRuntimeException(CommonStatusCode.USER_INFO_NOT_FULL.getStatusCode(),
                    CommonStatusCode.USER_INFO_NOT_FULL.getStatusMessage());
        }
        Activity activity = activityService.findByActivityCode(activityCode);

        if (null == answer || StringUtils.isBlank(answer) || "null".equals(answer))
        {
            GroupVolunteer groupVolunteer = this.groupVolunteerService.findByMemberAndGroup(memberCode, DKQConstant.APPLY_STATUS_OK, activity.getGroup().getGroupCode());
            if (null == groupVolunteer) {
                throw new ServiceRuntimeException("本活动暂未对外招募");
            } else {
                if (groupVolunteer.getStatus() == DKQConstant.APPLY_STATUS_FREEZE) {
                    throw new ServiceRuntimeException("已被冻结,不可以报名本社团活动");
                }
            }
            ActivityApply activityApply = new ActivityApply();
            activityApply.setCreateTime(Clock.DEFAULT.getCurrentDate());
            activityApply.setVolunteer(volunteer);
            activityApply.setActivity(activity);
            activityApply.setRole(DKQConstant.ROLES_VOLUNTEER);
            activityApply.setStatus(DKQConstant.APPLY_STATUS_OK);
            return this.activityApplyService.save(activityApply);
        } else {
//            GroupVolunteer groupVolunteer = this.groupVolunteerService.findByMemberAndGroup(memberCode,DKQConstant.APPLY_STATUS_FREEZE,activity.getGroup().getGroupCode());
//            if(null != groupVolunteer && groupVolunteer.getStatus() == DKQConstant.APPLY_STATUS_FREEZE)
//            {
//                throw new ServiceRuntimeException("已被冻结,不可以报名本社团活动");
//            }
            ActivityRecruit activityRecruit = this.activityRecruitService.findByActivityCode(activityCode);
            ActivityRecruitVolunteer activityRecruitVolunteer = new ActivityRecruitVolunteer();
            activityRecruitVolunteer.setStatus(DKQConstant.APPLY_STATUS);
            activityRecruitVolunteer.setCreateTime(Clock.DEFAULT.getCurrentDate());
            activityRecruitVolunteer.setActivityRecruit(activityRecruit);
            activityRecruitVolunteer.setVolunteer(volunteer);
            activityRecruitVolunteer.setAnswer(answer);
            activityRecruitVolunteer.setQuestion(activityRecruit.getQuestion());
            return this.saveApply(activityRecruitVolunteer);
        }
    }

    @Override
    public ActivityApply saveApply(ActivityRecruitVolunteer activityRecruitVolunteer) throws ServiceRuntimeException {
        //检查当前用户是否已报名参加本次活动
        ActivityApply aa = activityApplyService.findByVolunteerAndActivity(activityRecruitVolunteer.getVolunteer(), activityRecruitVolunteer.getActivityRecruit().getActivity());
        if (null != aa && aa.getStatus() == DKQConstant.APPLY_STATUS_OK) {
            throw new ServiceRuntimeException("您已报名过本次活动,不可以重复报名");
        }

        //检查当前用户是否已申请过当前活动
        ActivityRecruitVolunteer temp = this.isApply(activityRecruitVolunteer.getActivityRecruit().getId(), activityRecruitVolunteer.getVolunteer().getMemberCode());
        if (null != temp && temp.getStatus() != DKQConstant.APPLY_STATUS_OK) {
            if (temp.getStatus() == DKQConstant.APPLY_STATUS)
                throw new ServiceRuntimeException("请耐心等待管理员审核");
            else {
                temp.setStatus(DKQConstant.APPLY_STATUS);
                activityRecruitVolunteerDao.save(temp);
                saveLog("用户【" + temp.getVolunteer().getId() + "】报名参加一个活动招募 【" + temp.getId() + "】");
                ActivityRecruit activityRecruit = temp.getActivityRecruit();
                activityRecruit.setApplys(activityRecruit.getApplys() + 1);
                this.activityRecruitService.update(activityRecruit);

                //若是本社团成员则直接报名成功
                ActivityApply activityApply = new ActivityApply();
                activityApply.setRole(DKQConstant.ROLES_VOLUNTEER);
                activityApply.setStatus(DKQConstant.APPLY_STATUS);
                activityApply.setVolunteer(activityRecruitVolunteer.getVolunteer());
                activityApply.setCreateTime(Clock.DEFAULT.getCurrentDate());
                activityApply.setActivity(activityRecruitVolunteer.getActivityRecruit().getActivity());
                activityApplyService.save(activityApply);

                PushActivityMesssage.getInstance().applyActivityRecruit(temp.getActivityRecruit().getActivity().getName(), temp.getVolunteer().getMemberCode());
                PushActivityMesssage.getInstance().verfierActivityRecruit(temp.getVolunteer().getRealName(), temp.getVolunteer().getMemberCode(), temp.getActivityRecruit().getActivity().getName(), temp.getActivityRecruit().getActivity().getMonitor().getMemberCode(), temp.getActivityRecruit().getActivity().getActivityCode());
                return activityApply;
            }
        } else {
            //保存活动招募报名记录
            activityRecruitVolunteerDao.save(activityRecruitVolunteer);

            //更新本次招募报名人数
            saveLog("用户【" + activityRecruitVolunteer.getVolunteer().getId() + "】报名参加一个活动招募 【" + activityRecruitVolunteer.getId() + "】");
            ActivityRecruit activityRecruit = activityRecruitVolunteer.getActivityRecruit();
            activityRecruit.setApplys(activityRecruit.getApplys() + 1);
            this.activityRecruitService.update(activityRecruit);

            //若是本社团成员则直接报名成功
            ActivityApply activityApply = new ActivityApply();
            activityApply.setRole(DKQConstant.ROLES_VOLUNTEER);
            activityApply.setStatus(DKQConstant.APPLY_STATUS);
            activityApply.setVolunteer(activityRecruitVolunteer.getVolunteer());
            activityApply.setCreateTime(Clock.DEFAULT.getCurrentDate());
            activityApply.setActivity(activityRecruitVolunteer.getActivityRecruit().getActivity());
            activityApplyService.save(activityApply);

            saveLog("志愿者【" + activityApply.getVolunteer().getId() + "】通过招募报名参加" + activityApply.getActivity().getName() + "活动【" + activityApply.getActivity().getId() + "】,招募报名记录编号为【" + activityRecruitVolunteer.getId() + "】,审核状态为：" + activityRecruitVolunteer.getStatus());
            //PushActivityMesssage.getInstance().agreeActivityRecruit(activityApply.getActivity().getName(),activityApply.getVolunteer().getMemberCode());
            PushActivityMesssage.getInstance().applyActivityRecruit(activityRecruitVolunteer.getActivityRecruit().getActivity().getName(), activityRecruitVolunteer.getVolunteer().getMemberCode());
            PushActivityMesssage.getInstance().verfierActivityRecruit(activityRecruitVolunteer.getVolunteer().getRealName(), activityRecruitVolunteer.getVolunteer().getMemberCode(), activityRecruitVolunteer.getActivityRecruit().getActivity().getName(), activityRecruitVolunteer.getActivityRecruit().getActivity().getMonitor().getMemberCode(), activityRecruitVolunteer.getActivityRecruit().getActivity().getActivityCode());

            return activityApply;
        }
    }

    @Override
    public void save(ActivityRecruitVolunteer activityRecruitVolunteer) throws ServiceRuntimeException {
        //检查当前用户是否已报名参加本次活动
        ActivityApply aa = activityApplyService.findByVolunteerAndActivity(activityRecruitVolunteer.getVolunteer(), activityRecruitVolunteer.getActivityRecruit().getActivity());
        if (null != aa && aa.getStatus() == DKQConstant.APPLY_STATUS_OK) {
            throw new ServiceRuntimeException("您已报名过本次活动,不可以重复报名");
        }

        //检查当前用户是否已申请过当前活动
        ActivityRecruitVolunteer temp = this.isApply(activityRecruitVolunteer.getActivityRecruit().getId(), activityRecruitVolunteer.getVolunteer().getMemberCode());
        if (null != temp && temp.getStatus() != DKQConstant.APPLY_STATUS_OK) {
            if (temp.getStatus() == DKQConstant.APPLY_STATUS)
                throw new ServiceRuntimeException("请耐心等待管理员审核");
            else {
                temp.setStatus(DKQConstant.APPLY_STATUS);
                activityRecruitVolunteerDao.save(temp);
                saveLog("用户【" + temp.getVolunteer().getId() + "】报名参加一个活动招募 【" + temp.getId() + "】");
                ActivityRecruit activityRecruit = temp.getActivityRecruit();
                activityRecruit.setApplys(activityRecruit.getApplys() + 1);
                this.activityRecruitService.update(activityRecruit);
                PushActivityMesssage.getInstance().applyActivityRecruit(temp.getActivityRecruit().getActivity().getName(), temp.getVolunteer().getMemberCode());
                PushActivityMesssage.getInstance().verfierActivityRecruit(temp.getVolunteer().getRealName(), temp.getVolunteer().getMemberCode(), temp.getActivityRecruit().getActivity().getName(), temp.getActivityRecruit().getActivity().getMonitor().getMemberCode(), temp.getActivityRecruit().getActivity().getActivityCode());
            }
        } else {
            //保存活动招募报名记录
            activityRecruitVolunteerDao.save(activityRecruitVolunteer);

            //更新本次招募报名人数
            saveLog("用户【" + activityRecruitVolunteer.getVolunteer().getId() + "】报名参加一个活动招募 【" + activityRecruitVolunteer.getId() + "】");
            ActivityRecruit activityRecruit = activityRecruitVolunteer.getActivityRecruit();
            activityRecruit.setApplys(activityRecruit.getApplys() + 1);
            this.activityRecruitService.update(activityRecruit);

            //若是本社团成员则直接报名成功
            ActivityApply activityApply = new ActivityApply();
            activityApply.setRole(DKQConstant.ROLES_VOLUNTEER);
            activityApply.setStatus(activityRecruitVolunteer.getStatus());
            activityApply.setVolunteer(activityRecruitVolunteer.getVolunteer());
            activityApply.setCreateTime(Clock.DEFAULT.getCurrentDate());
            activityApply.setActivity(activityRecruitVolunteer.getActivityRecruit().getActivity());
            activityApplyService.save(activityApply);

            saveLog("志愿者【" + activityApply.getVolunteer().getId() + "】通过招募报名参加" + activityApply.getActivity().getName() + "活动【" + activityApply.getActivity().getId() + "】,招募报名记录编号为【" + activityRecruitVolunteer.getId() + "】,审核状态为：" + activityRecruitVolunteer.getStatus());
            PushActivityMesssage.getInstance().agreeActivityRecruit(activityApply.getActivity().getName(), activityApply.getVolunteer().getMemberCode());
            PushActivityMesssage.getInstance().applyActivityRecruit(activityRecruitVolunteer.getActivityRecruit().getActivity().getName(), activityRecruitVolunteer.getVolunteer().getMemberCode());
            PushActivityMesssage.getInstance().verfierActivityRecruit(activityRecruitVolunteer.getVolunteer().getRealName(), activityRecruitVolunteer.getVolunteer().getMemberCode(), activityRecruitVolunteer.getActivityRecruit().getActivity().getName(), activityRecruitVolunteer.getActivityRecruit().getActivity().getMonitor().getMemberCode(), activityRecruitVolunteer.getActivityRecruit().getActivity().getActivityCode());
        }
    }

    @Override
    public void update(ActivityRecruitVolunteer activityRecruitVolunteer) throws ServiceRuntimeException {
        activityRecruitVolunteerDao.save(activityRecruitVolunteer);
    }

    @Override
    public Page<ActivityRecruitVolunteer> findAll(Long activityRecruitId, int pageNumber) throws ServiceRuntimeException {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("activityRecruit.id", new SearchFilter("activityRecruit.id", SearchFilter.Operator.EQ, activityRecruitId));
        Specification<ActivityRecruitVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityRecruitVolunteer.class);
        return this.activityRecruitVolunteerDao.findAll(spec, pageRequest);
    }

    @Override
    public Page<ActivityRecruitVolunteer> findByActivityRecruit(Long activityRecruitId, int status, int pageNumber) throws ServiceRuntimeException {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("activityRecruit.id", new SearchFilter("activityRecruit.id", SearchFilter.Operator.EQ, activityRecruitId));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));
        Specification<ActivityRecruitVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityRecruitVolunteer.class);
        return this.activityRecruitVolunteerDao.findAll(spec, pageRequest);
    }

    @Override
    public Page<ActivityRecruitVolunteer> findByActivityRecruit(String activityCode, int status, int pageNumber) throws ServiceRuntimeException {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("activityRecruit.activity.activityCode", new SearchFilter("activityRecruit.activity.activityCode", SearchFilter.Operator.EQ, activityCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));
        Specification<ActivityRecruitVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityRecruitVolunteer.class);
        return this.activityRecruitVolunteerDao.findAll(spec, pageRequest);
    }

    @Override
    public void updateStatus(Long activityRecruitVolunteerId, int status, String refuseCase) throws ServiceRuntimeException {
        ActivityRecruitVolunteer activityRecruitVolunteer = this.activityRecruitVolunteerDao.findOne(activityRecruitVolunteerId);
        if (activityRecruitVolunteer != null) {
            activityRecruitVolunteer.setStatus(status);
            this.activityRecruitVolunteerDao.save(activityRecruitVolunteer);

            ActivityApply activityApply = this.activityApplyService.findByVolunteerAndActivityAndStatus(activityRecruitVolunteer.getVolunteer().getMemberCode(), activityRecruitVolunteer.getActivityRecruit().getActivity().getActivityCode(), DKQConstant.APPLY_STATUS);
            if (status == DKQConstant.APPLY_STATUS_OK) {
//                //检查当前用户是否已报名参加本次活动
//                ActivityApply aa = activityApplyService.findByVolunteerAndActivity(activityRecruitVolunteer.getVolunteer(), activityRecruitVolunteer.getActivityRecruit().getActivity());
//                if(null != aa)
//                {
//                    throw new ServiceRuntimeException("该用户已报名过本次活动,不可以重复报名");
//                }
//                ActivityApply activityApply = new ActivityApply();
//                activityApply.setRole(DKQConstant.ROLES_VOLUNTEER);
//                activityApply.setStatus(DKQConstant.APPLY_STATUS_OK);
//                activityApply.setVolunteer(activityRecruitVolunteer.getVolunteer());
//                activityApply.setCreateTime(Clock.DEFAULT.getCurrentDate());
//                activityApply.setActivity(activityRecruitVolunteer.getActivityRecruit().getActivity());
//                activityApplyService.save(activityApply);

                saveLog("管理员审核一个活动招募报名记录【" + activityRecruitVolunteer.getId() + "】,审核状态为：" + status);
                PushActivityMesssage.getInstance().agreeActivityRecruit(activityApply.getActivity().getName(), activityApply.getVolunteer().getMemberCode());
            } else if (status == DKQConstant.APPLY_STATUS_REFUST) {
                saveLog("管理员审核一个活动招募报名记录【" + activityRecruitVolunteer.getId() + "】,审核状态为：" + status + ",拒绝理由为 " + refuseCase);
                activityRecruitVolunteer.setRefuseCase(activityRecruitVolunteer.getRefuseCase() + "|" + refuseCase);
                PushSystemMesssage.getInstance().refuseActivityRecruit(activityRecruitVolunteer.getActivityRecruit().getActivity().getName(), activityRecruitVolunteer.getVolunteer().getMemberCode());
            }
            if (null != activityApply)
            {
                activityApply.setStatus(status);
                this.activityApplyService.update(activityApply);

                //更新活动的报名人数
                if (activityApply.getStatus() == DKQConstant.APPLY_STATUS_OK)
                {
                    Activity activity = activityApply.getActivity();
                    activity.setApplys(activity.getApplys() + 1);
                    this.activityService.update(activity);
                    joinGroupMesssage(activityApply.getVolunteer().getMemberCode(), activityApply.getVolunteer().getNickName(), activityApply.getActivity().getActivityCode(), activityApply.getActivity().getName());
                }

            }
        }
    }
    private void joinGroupMesssage(String userId,String userName,String groupCode,String groupName)
    {
        PushGroupMesssage.getInstance().joinGroup("A",userId, userName,groupCode, groupName);
    }
    @Override
    public void updateStatus(String param) throws ServiceRuntimeException {
        JSONObject jsonObject = JSON.parseObject(param);
        Long activityRecruitVolunteerId = jsonObject.getLong("activityRecruitVolunteerId");
        String refuseCase = jsonObject.getString("refuseCase");
        String memberCode = jsonObject.getString("memberCode");
        int status = jsonObject.getIntValue("status");
        this.updateStatus(activityRecruitVolunteerId, status, refuseCase);
    }

    @Override
    public ActivityRecruitVolunteer findOne(Long activityRecruitVolunteerId) throws ServiceRuntimeException {
        return this.activityRecruitVolunteerDao.findOne(activityRecruitVolunteerId);
    }

    @Override
    public ActivityRecruitVolunteer findOne(String activityCode, String memberCode) throws ServiceRuntimeException {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("activityRecruit.activity.activityCode", new SearchFilter("activityRecruit.activity.activityCode", SearchFilter.Operator.EQ, activityCode));
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.APPLY_STATUS));
        Specification<ActivityRecruitVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityRecruitVolunteer.class);
        List<ActivityRecruitVolunteer> list = this.activityRecruitVolunteerDao.findAll(spec);
        if (null != list && list.size() > 0)
            return list.get(0);
        return null;
    }

    @Override
    public ActivityRecruitVolunteer isApply(Long activityRecruitId, String memberCode) throws ServiceRuntimeException {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("activityRecruit.id", new SearchFilter("activityRecruit.id", SearchFilter.Operator.EQ, activityRecruitId));
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        Specification<ActivityRecruitVolunteer> spec = DynamicSpecifications.bySearchFilter(filters.values(), ActivityRecruitVolunteer.class);
        List<ActivityRecruitVolunteer> list = this.activityRecruitVolunteerDao.findAll(spec);
        if (null != list && list.size() > 0)
            return list.get(0);
        return null;
    }

    @Override
    public void exit(String param) throws ServiceRuntimeException {
        JSONObject jsonObject = JSON.parseObject(param);
        String memberCode = jsonObject.getString("memberCode");
        Long activityRecruitId = jsonObject.getLong("activityRecruitId");
        exit(isApply(activityRecruitId, memberCode));
    }

    @Override
    public void exit(ActivityRecruitVolunteer activityRecruitVolunteer) throws ServiceRuntimeException {
        if (null != activityRecruitVolunteer) {
            ActivityApply activityApply = this.activityApplyService.findByVolunteerAndActivityAndStatus(activityRecruitVolunteer.getVolunteer().getMemberCode(), activityRecruitVolunteer.getActivityRecruit().getActivity().getActivityCode(), DKQConstant.APPLY_STATUS);
            if (null != activityApply) {
                activityApply.setStatus(DKQConstant.APPLY_STATUS_EXIT);
                activityApply.setRefuseCase("自己退出招募");
                this.activityApplyService.update(activityApply);
            }

            saveLog(activityRecruitVolunteer.getVolunteer() + "退出" + activityRecruitVolunteer.getActivityRecruit().getActivity().getName() + "招募报名");
            ActivityRecruit activityRecruit = activityRecruitVolunteer.getActivityRecruit();
            activityRecruit.setApplys(activityRecruit.getApplys() - 1);
            this.activityRecruitService.update(activityRecruit);
            activityRecruitVolunteerDao.delete(activityRecruitVolunteer);


        }
    }
}
